﻿#[macro_use]
extern crate log;
#[macro_use]
extern crate lazy_static;

use anyhow;
use apiexts::CustomResourceDefinition;
use either::Either::{Left, Right};
use futures::{StreamExt, TryStreamExt};
use k8s_openapi::api::apps::v1::{Deployment, DeploymentSpec, DeploymentStatus};
use k8s_openapi::api::core::v1::Pod;
use k8s_openapi::api::core::v1::Service;
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1beta1 as apiexts;
use kube::client::ConfigExt;
use kube::{
    self,
    api::{Api, DeleteParams, ListParams, Patch, PatchParams, PostParams, ResourceExt, WatchEvent},
    core::crd::v1beta1::CustomResourceExt,
    Client, Config, CustomResource,
};
use k8s_openapi::api::extensions::v1beta1::{Ingress,IngressRule};
use tracing;
use tracing_subscriber;

extern crate hyper;
extern crate hyper_tls;
extern crate tower;
use serde::{Deserialize, Serialize};
use std::io::{Read, Write};
use std::net;
use std::net::Ipv4Addr;
use std::result::Result::Ok;
use std::sync::mpsc::channel;
use std::thread::spawn;
use std::time::Duration;
//k8s  client封装
mod k8s_client;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
    std::env::set_var("RUST_LOG", "info,kube=debug");
    tracing_subscriber::fmt::init();

    //k8s客户端
    let client = k8s_client::init_k8s_client().await?;

    
    //let namespace = "kube-system".to_owned();
    //测试查询pods列表
    //let pods: Api<Pod> = Api::namespaced(client, &namespace);
    let ingress_list: Api<Ingress> = Api::all(client);
    let lp = ListParams::default()
        //.fields(&format!("labels.k8s-app={}", "metrics-server"))
        .timeout(10);

    for p in ingress_list.list(&lp).await? {
        let ns =p.namespace().unwrap();
        let ingress_name =p.name();
        //tracing::info!("Namespace:{} ,Found ingress: {}",&ns ,&ingress_name);
        let spec =p.spec.unwrap();
        let  ingress_rules :Vec<IngressRule>= spec.rules.unwrap();

        for rule in ingress_rules {
             //tracing::info!("rule:{:?}",&rule);
             match rule.host {
                 Some(_h) => {
                   // tracing::info!("hostname: {}", h);
                 },
                 None => {
                    tracing::info!("Namespace:{} ,Found ingress: {}",&ns ,&ingress_name);
                    tracing::error!("no hostname config!!!");
                 }
             }
        }

        let tls = spec.tls ;

        match  tls {
            Some(_t) => {
             },
             None => {
                tracing::info!("Namespace:{} ,Found ingress: {}",&ns ,&ingress_name);
                tracing::error!("no tls config!!!");
             }
        }
    }
    

    Ok(())
}
